<template>
  <div class="network-visualization">
    <!-- Header -->
    <header class="header">
      <h1 class="title">物联网设备管理拓扑图</h1>
      <div class="time">
        {{ new Date().toLocaleString().replaceAll("/", "-") }}
      </div>
    </header>

    <!-- Main content -->
    <div class="content">
      <!-- Toolbar -->
      <div class="toolbar">
        <div class="search-area">
          <div class="search-item">
            <span>设备名称：</span>
            <input type="text" class="search-input" v-model="searchDevice" />
          </div>
          <div class="search-item">
            <span>设备编号：</span>
            <input type="text" class="search-input" v-model="searchSerial" />
          </div>
          <button class="save-btn" @click="saveTopology">保存</button>
        </div>
      </div>

      <!-- Device palette -->
      <DevicePalette :deviceTypes="deviceTypes" @add-device="addDevice" />

      <!-- Topology canvas -->
      <TopologyGraph @itemClick="handleItemClick"></TopologyGraph>
      <!-- <Topology></Topology> -->
      <!-- Device details panel -->
      <DeviceDetails v-if="selectedDevice" :device="selectedDevice" @close="closePanel" />
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
import DevicePalette from "./components/DevicePalette.vue";
import DeviceDetails from "./components/DeviceDetails.vue";
import TopologyGraph from "./components/TopologyGraph.vue";
import { useDevice } from "./composables/useDevice";
const searchDevice = ref("");
const searchSerial = ref("");
// 设备逻辑
const { devices, deviceTypes, addDevice, selectedDevice, closePanel } =
  useDevice();
//点击拓扑图
const handleItemClick = (item) => {
  console.log("点击了拓扑图", item);
  selectedDevice.value = item;
}
// 保存拓扑图
const saveTopology = () => {
  const data = {
    devices: devices.value,
    connections: connections.value,
  };
  const blob = new Blob([JSON.stringify(data)], { type: "application/json" });
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = "topology.json";
  a.click();
  URL.revokeObjectURL(url);
};

// 示例连接
const connections = ref([
  { id: 1, source: devices.value[1], target: devices.value[0], active: true },
  {
    id: 2,
    source: devices.value[2],
    target: devices.value[0],
    active: false,
  },
]);
</script>

<style lang="scss" scoped>
.network-visualization {
  background-color: #000b11;
  background-image: url("../../assets/index/bg.png");
  background-size: 100% 100%;
  height: calc(100vh - 100px);
  border-radius: 4px 4px 4px 4px;
  position: relative;

  header {
    height: 63px;
    text-align: center;
    color: #fff;
    background-image: url("../../assets/index/titleBg.png");
    background-size: 100% 100%;

    .time {
      position: absolute;
      right: 37px;
      top: 10px;
    }

    .title {
      line-height: 40px;
    }
  }

  .content {
    color: #fff;
    height: calc(100vh - 163px);
    box-sizing: border-box;
    position: relative;

    .search-area {
      display: flex;
      align-items: center;
      justify-content: flex-end;
      position: absolute;
      right: 38px;
      top: 24px;
      z-index: 11;

      .search-item+.search-item {
        margin-left: 32px;
      }

      .search-item {
        font-weight: 400;
        font-size: 16px;
        color: #ffffff;
        line-height: 24px;

        input {
          width: 68px;
          height: 26px;
          padding: 10px;
          box-sizing: border-box;
          background: rgba(255, 255, 255, 0);
          border-radius: 2px 2px 2px 2px;
          border: 1px solid #49a7e7;
          outline: none;
          color: #fff;
        }
      }

      .save-btn {
        width: 82px;
        height: 38px;
        margin-left: 32px;
        background: linear-gradient(180deg, #102557 22%, #204aa9 100%);
        border-radius: 1px 1px 1px 1px;
        font-weight: bold;
        font-size: 14px;
        color: #ffffff;
        line-height: 16px;
        border: 1px solid #49a7e7;
      }
    }

    .topology-canvas {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
    }
  }
}
</style>
